package bookManage;

import java.util.ArrayList;
import java.util.Scanner;

public class BookTest {
	static ArrayList<RebackModel> rebacks = new ArrayList<RebackModel>();
	static ArrayList<BorrowModel> borrows = new ArrayList<BorrowModel>();
	
	static Scanner scanner = new Scanner(System.in);
	
	//管理员
	public void manage() {
		CardModel cardModel = new CardModel();
		RebackModel rebackModel = new RebackModel();
		BookModel bookModel = new BookModel();
		BorrowModel borrowModel = new BorrowModel();
		while (true) {
			System.out.println("请选择");
			System.out.println("-----------------------");
			System.out.println("1-进书");
			System.out.println("2-全部图书信息");
			System.out.println("3-借阅记录");
			System.out.println("4-归还记录");
			System.out.println("5-办理借阅卡");
			System.out.println("6-全部用户信息");
			System.out.println("7-退出");
			System.out.println("-----------------------");
			
			int key = Integer.parseInt(scanner.nextLine());

			if (key == 7) {
				break;
			}
			switch (key) {
			case 1:
				System.out.println("请输入要添加的图书编号：");
				int id = Integer.parseInt(scanner.nextLine());
				System.out.println("请输入要添加的图书名：");
				String namee = scanner.nextLine();
				bookModel.addBook(id, namee);
				break;
			case 2:
				bookModel.selectAll();
				break;
			case 3:
				borrowModel.selectAll();
				break;
			case 4:
				rebackModel.selectAll();
				break;
			case 5:
				addCard();
				break;
			case 6:
				cardModel.selectAll();
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			scanner.nextLine();
		}
	}
	
	
	//用户
	/**
	 * 注册
	 */
	public void addCard() {
		CardModel cardModel = new CardModel();

		System.out.println("========注册========");
		System.out.print("请输入借阅卡id：");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.print("请输入借阅卡密码：");
		String psw = scanner.nextLine();

		cardModel.addCard(id, psw);
	}
	
	/**
	 * 登录
	 */
	public void login() {
		CardModel cardModel = new CardModel();

		System.out.println("========登录========");
		System.out.print("请输入借阅卡id：");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.print("请输入借阅卡密码：");
		String psw = scanner.nextLine();

		CardModel card = cardModel.selectById(id);
		if (card != null) {
			boolean b = card.judge(card, psw);// 密码是否正确
			if (b) {
				Card.userId = id;
				System.out.println("登录成功！");
				System.out.println();

				userList();
				return;
			}
		}

		System.out.println("借阅卡id或密码错误！");
		System.out.println("点击回车继续登录（取消登录输入 N）...");
		String x = scanner.nextLine();
		if (x.equals("N"))
			return;
		login();
	}
	
	/**
	 * 首页
	 */
	public void list() {
		while (true) {
			System.out.println("请选择");
			System.out.println("-----------------------");
			System.out.println("1-登录");
			System.out.println("2-注册");
			System.out.println("3-管理员");
			System.out.println("4-退出");
			System.out.println("-----------------------");

			int key = Integer.parseInt(scanner.nextLine());
			if (key == 4) {
				return;
			}
			switch (key) {
			case 1:
				login();
				break;
			case 2:
				addCard();
				break;
			case 3:
				manage();
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			scanner.nextLine();
		}
	}


	/**
	 * 用户列表
	 */
	public void userList() {
		CardModel cardModel = new CardModel();
		BorrowModel borrowModel = new BorrowModel();
		Card card = new Card();
		RebackModel rebackModel = new RebackModel();
		while (true) {
			System.out.println("请选择");
			System.out.println("-----------------------");
			System.out.println("1-修改密码");
			System.out.println("2-全部信息");
			System.out.println("3-借阅图书");
			System.out.println("4-归还图书");
			System.out.println("5-查询个人借阅记录");
			System.out.println("6-查询个人借阅记录");
			System.out.println("7-查询图书借阅记录");
			System.out.println("8-退出");
			System.out.println("-----------------------");

			int key = Integer.parseInt(scanner.nextLine());

			if (key == 8) {
				break;
			}
			switch (key) {
			case 1:
				card.updatePsw();
				break;
			case 2:
				cardModel.selectAll();
				break;
			case 3:
				card.borrowBook();
				break;
			case 4:
				card.rebackBook();
				break;
			case 5:
				borrowModel.selectByUser();
				break;
			case 6:
				rebackModel.selectByUser();
				break;
			case 7:
				System.out.println("请输入要查询的图书名：");
				String name = scanner.nextLine();
				borrowModel.selectByBook(name);
			default:
				break;
			}
			System.out.println("点击回车继续...");
			scanner.nextLine();
		}
	}
	
	
	public static void main(String[] args) {
		Book book = new Book();
		BookTest bookTest = new BookTest();
		
		book.init();
		bookTest.list();
	}
}
